package com.my.socket.bio;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.net.ServerSocketFactory;
import java.net.ServerSocket;
import java.util.concurrent.FutureTask;

/**
 * User: Alex Zhong
 * Date: 2016/6/17
 */
public class Server {
    private static final Logger LOGGER = LoggerFactory.getLogger(Server.class);

    private int port;

    public Server(int port) {
        this.port = port;
    }

    public static void main(String[] args) throws Exception {
        LOGGER.info("startup an accept thread to monitor the client connection");

        Server server = new Server(9999);
        server.listen();

        LOGGER.info("go on to do the other things");
    }

    public void listen() throws Exception {
        ServerSocketFactory serverSocketFactory = ServerSocketFactory.getDefault();
        ServerSocket serverSocket = serverSocketFactory.createServerSocket(this.port);

        for (int i = 0; i < 5; i++) {
            AcceptCallable acceptCallable = new AcceptCallable(serverSocket);
            FutureTask futureTask = new FutureTask(acceptCallable);
            Thread thread = new Thread(futureTask);
            thread.start();
        }

    }
}
